# 导包
from api.home import Homeapi
from api.order import OrderAPI
from commit.dbutil import DBUtil


# 创建测试类
class TestYeWu:
    # 初始化
    goods_id = None
    order_id = None

    # 前置处理
    def setup(self):
        self.home_api = Homeapi()
        self.order_api = OrderAPI()
        self.db_util = DBUtil()

    # 01：获取轮播图信息
    def test01_get_banner_info(self):
        response = self.home_api.carousel()
        # print(response.json())

        # 提取商品id并保存
        TestYeWu.goods_id = response.json().get("items")[0].get("key_word")
        print(TestYeWu.goods_id)

    # 02: 创建订单
    def test02_add_order(self):
        # 当要测试库存不足时，先连接数据库修改商品的库存数量如5
        add_data = {
            "products": [
                {
                    "product_id": TestYeWu.goods_id,
                    "count": 6
                }
            ]
        }
        response = self.order_api.get_add_order(add_data)
        print(response.json())

        # 提取订单编号并保存类的属性
        TestYeWu.order_id = response.json().get("order_id")
        print(TestYeWu.order_id)

    # 03：查看订单（检查支付状态）
    def test03_get_order_info(self):
        response = self.order_api.get_order(TestYeWu.order_id)
        print(response.json())

        # 04：连接数据库修改支付状态（1未付款-》2已支付）
        self.db_util.exe_sql(f"UPDATE `order` set status =2 where id ={TestYeWu.order_id};")

    # 05：获取订单列表 或 查看订单
    def test05_get_order_info(self):
        response = self.order_api.get_order(TestYeWu.order_id)
        print(response.json())
